/*
 * Copyright (C) 2021 The Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.oushangfeng.pinneddemo.custom.categorylist;

import com.oushangfeng.pinneddemo.ResourceTable;
import com.oushangfeng.pinneddemo.datainterface.CategoryItemBase;
import com.oushangfeng.pinneddemo.util.ToastUtil;

import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;

/**
 * SmallLabelItem
 *
 * @since 2021-03-27
 * Created by oubowu on 2021/3/27 10:29
 * Head item which display a category name in the list container item.
 */
public class SmallLabelItem implements CategoryItemBase {
    /**
     * HeadItem type
     */
    public static final int ITEM_TYPE = 3;
    private int type = SearchItem.ITEM_TYPE;
    private final Context context;
    private String leftText;
    private String headText;
    private String picIndex;
    private String itemType;
    private ToastUtil toastUtil;
    /**
     * SmallLabelItem
     *
     * @param context
     * @param type
     * @param leftText
     * @param headText
     * @param picIndex
     * @param itemType
     */
    SmallLabelItem(Context context, int type, String leftText, String headText, String picIndex, String itemType) {
        this.context = context;
        this.type = type;
        this.leftText = leftText;
        this.headText = headText;
        this.picIndex = picIndex;
        this.itemType = itemType;
        toastUtil = new ToastUtil(context);
    }

    /**
     * Set component content
     *
     * @param component the component to set
     */
    @Override
    public void bindComponent(Component component) {
        DependentLayout dlLayout = (DependentLayout) component.findComponentById(ResourceTable.Id_item_dl_small_all);

        Image image = (Image) component.findComponentById(ResourceTable.Id_item_right_icon);
        image.setPixelMap(Integer.parseInt(headText));
        PixelMap bitmap = getPixelMap(Integer.parseInt(headText));
        int hi = bitmap.getImageInfo().size.height;
        int wi = bitmap.getImageInfo().size.width;
        int dlHeight = dlLayout.getHeight();
        int bl = dlHeight / hi;
        image.setHeight(dlHeight);
        image.setWidth(bl * wi);
        Image image2 = (Image) component.findComponentById(ResourceTable.Id_small_label_icon);
        image2.setPixelMap(Integer.parseInt(headText));
        image2.setScaleMode(Image.ScaleMode.STRETCH);
        if (!"1".equals(picIndex)) {
            image2.setVisibility(Image.HIDE);
        }

        Text contentText = (Text) component.findComponentById(ResourceTable.Id_item_content_text);
        contentText.setText(leftText);

        image2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                toastUtil.toast(context, "click,tag:" + itemType);
            }
        });

        image2.setDoubleClickedListener(new Component.DoubleClickedListener() {
            @Override
            public void onDoubleClick(Component component) {
                toastUtil.toast(context, "click,tag:" + itemType);
            }
        });
    }

    /**
     * Create component
     *
     * @return component
     */
    @Override
    public Component createComponent() {
        return LayoutScatter.getInstance(context).parse(ResourceTable.Layout_small_label,
                null, false);
    }

    /**
     * Get the item type of this category item
     *
     * @return category item type
     */
    @Override
    public int getItemType() {
        return type;
    }

    /**
     * getItem
     *
     * @return headText
     */
    @Override
    public String getItem() {
        return headText;
    }

    /**
     * setItem
     *
     * @param value
     */
    @Override
    public void setItem(String value) {
        headText = value;
    }

    /**
     * getItemName
     *
     * @return getItemName itemType
     */
    @Override
    public String getItemName() {
        return itemType;
    }

    /**
     * 通过资源ID获取位图对象
     *
     * @param resId
     * @return getPixelMap
     */
    private PixelMap getPixelMap(int resId) {
        PixelMap nuPm = null;
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = context.getResourceManager().getResource(resId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/jpg";
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            ImageSource imageSource = ImageSource.create(drawableInputStream, null);
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (NotExistException | IOException e) {
            toastUtil.toast(context, "Exception");
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (IOException e) {
                toastUtil.toast(context, "FIN Exception");
            }
        }
        return nuPm;
    }
}
